/*     */ package com.zimbra.cs.account.ldap;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import com.zimbra.cs.account.AttributeManager.IDNType;
/*     */ import com.zimbra.cs.account.AttributeType;
/*     */ import com.zimbra.cs.account.IDNUtil;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import java.util.Map;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ class LdapGalMapRule
/*     */ {
/*     */   private String[] mLdapAttrs;
/*     */   private String[] mContactAttrs;
/*     */   private LdapGalValueMap[] mContactAttrsValueMaps;
/*     */   private boolean mIsBinary;
/*     */   private boolean mIsBinaryTransfer;
/*     */   private boolean mIsSMIMECertificate;
/*  47 */   private static final Pattern typedRule = Pattern.compile("\\((.*)\\)\\s(.*)");
/*     */   
/*     */   public LdapGalMapRule(String rule, Map<String, LdapGalValueMap> valueMaps)
/*     */   {
/*  51 */     Matcher matcher = typedRule.matcher(rule);
/*  52 */     if (matcher.matches()) {
/*  53 */       String type = matcher.group(1);
/*  54 */       AttributeType attrType = AttributeType.getType(type);
/*  55 */       if (attrType == null) {
/*  56 */         ZimbraLog.gal.warn("Unrecognized type in attr map: " + type + ", type is ignore for rule " + rule);
/*     */       } else {
/*  58 */         if (AttributeManager.isBinaryType(attrType)) {
/*  59 */           this.mIsBinary = true;
/*  60 */         } else if (AttributeManager.isBinaryTransferType(attrType)) {
/*  61 */           this.mIsBinaryTransfer = true;
/*     */         }
/*     */         
/*     */ 
/*  65 */         rule = matcher.group(2);
/*     */       }
/*     */     }
/*     */     
/*  69 */     int p = rule.indexOf('=');
/*  70 */     if (p != -1) {
/*  71 */       String ldapAttr = rule.substring(0, p);
/*  72 */       String contactAttr = rule.substring(p + 1);
/*     */       
/*  74 */       this.mLdapAttrs = new String[] { ldapAttr.indexOf(',') != -1 ? ldapAttr.split(",") : ldapAttr };
/*  75 */       this.mContactAttrs = new String[] { contactAttr.indexOf(',') != -1 ? contactAttr.split(",") : contactAttr };
/*     */       
/*  77 */       this.mContactAttrsValueMaps = new LdapGalValueMap[this.mContactAttrs.length];
/*  78 */       if (valueMaps != null) {
/*  79 */         for (int i = 0; i < this.mContactAttrs.length; i++) {
/*  80 */           this.mContactAttrsValueMaps[i] = ((LdapGalValueMap)valueMaps.get(this.mContactAttrs[i]));
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*  85 */     for (String contactAttr : this.mContactAttrs) {
/*  86 */       if (Contact.isSMIMECertField(contactAttr)) {
/*  87 */         this.mIsSMIMECertificate = true;
/*  88 */         break;
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean isBinary() {
/*  94 */     return this.mIsBinary;
/*     */   }
/*     */   
/*     */   public boolean isBinaryTransfer() {
/*  98 */     return this.mIsBinaryTransfer;
/*     */   }
/*     */   
/*     */   public boolean containsBinaryData() {
/* 102 */     return (this.mIsBinary) || (this.mIsBinaryTransfer);
/*     */   }
/*     */   
/*     */   public boolean isSMIMECertificate()
/*     */   {
/* 107 */     return this.mIsSMIMECertificate;
/*     */   }
/*     */   
/*     */   public String[] getLdapAttrs() {
/* 111 */     return this.mLdapAttrs;
/*     */   }
/*     */   
/*     */   public String[] getContactAttrs() {
/* 115 */     return this.mContactAttrs;
/*     */   }
/*     */   
/*     */ 
/*     */   private int addToContactAttrs(Map<String, Object> contactAttrs, Object value, int index)
/*     */   {
/* 121 */     if (index >= this.mContactAttrs.length) return index;
/* 122 */     for (int i = 0; i < index; i++) {
/* 123 */       Object v = contactAttrs.get(this.mContactAttrs[i]);
/*     */       
/* 125 */       if (v != null) {
/* 126 */         value = mapValue(i, value);
/* 127 */         if (v.equals(value)) {
/* 128 */           return index;
/*     */         }
/*     */       }
/*     */     }
/* 132 */     contactAttrs.put(this.mContactAttrs[index], mapValue(index, value));
/* 133 */     index++;
/* 134 */     return index;
/*     */   }
/*     */   
/*     */   private Object mapValue(int index, Object value) {
/* 138 */     LdapGalValueMap valueMap = this.mContactAttrsValueMaps[index];
/* 139 */     if (valueMap != null) {
/* 140 */       return valueMap.apply(value);
/*     */     }
/* 142 */     return value;
/*     */   }
/*     */   
/*     */   void apply(IAttributes ldapAttrs, Map<String, Object> contactAttrs) {
/* 146 */     AttributeManager attrMgr = AttributeManager.getInst();
/*     */     
/* 148 */     int index = 0;
/* 149 */     for (String ldapAttr : this.mLdapAttrs) {
/* 150 */       if (index >= this.mContactAttrs.length)
/*     */         return;
/*     */       String[] val;
/* 153 */       try { val = ldapAttrs.getMultiAttrString(ldapAttr, containsBinaryData(), isBinaryTransfer());
/*     */       } catch (ServiceException e) {
/* 155 */         return;
/*     */       }
/*     */       
/* 158 */       AttributeManager.IDNType idnType = AttributeManager.idnType(attrMgr, ldapAttr);
/*     */       
/* 160 */       if (val.length == 1) {
/* 161 */         index = addToContactAttrs(contactAttrs, IDNUtil.toUnicode(val[0], idnType), index);
/* 162 */       } else if (val.length > 1) {
/* 163 */         if (this.mContactAttrs.length == 1) {
/* 164 */           index = addToContactAttrs(contactAttrs, val, index);
/* 165 */           return;
/*     */         }
/* 167 */         for (int i = 0; i < val.length; i++) {
/* 168 */           if (index >= this.mContactAttrs.length) return;
/* 169 */           index = addToContactAttrs(contactAttrs, IDNUtil.toUnicode(val[i], idnType), index);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/LdapGalMapRule.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */